// pages/consultation/comment/comment.js
Page({
  data: {
    consultation: {
      id: '12345',
      doctor: {
        avatar: '/images/doctor_avatar.jpg',
        name: '心理咨询师',
        title: '咨询师',
        department: '简介'
      },
      serviceType: '图文咨询',
      createTime: '2023-07-15'
    },
    rating: 0,
    ratingText: {
      1: '非常不满意',
      2: '不满意',
      3: '一般',
      4: '满意',
      5: '非常满意'
    },
    tags: [
      { id: 1, name: '专业可靠', selected: false },
      { id: 2, name: '回复及时', selected: false },
      { id: 3, name: '耐心细致', selected: false },
      { id: 4, name: '解答清晰', selected: false },
      { id: 5, name: '态度亲切', selected: false },
      { id: 6, name: '建议实用', selected: false }
    ],
    content: '',
    isAnonymous: false
  },

  // 星级评分
  handleRate(e) {
    const rating = e.currentTarget.dataset.rating;
    this.setData({ rating });
  },

  // 切换标签
  toggleTag(e) {
    const id = e.currentTarget.dataset.id;
    const tags = this.data.tags.map(item => {
      if (item.id === id) {
        item.selected = !item.selected;
      }
      return item;
    });
    this.setData({ tags });
  },

  // 输入评价内容
  handleInput(e) {
    this.setData({ content: e.detail.value });
  },

  // 切换匿名状态
  toggleAnonymous(e) {
    this.setData({ isAnonymous: e.detail.value.length > 0 });
  },

  // 提交评价
  handleSubmit() {
    if (this.data.rating === 0) {
      wx.showToast({
        title: '请先进行星级评价',
        icon: 'none'
      });
      return;
    }

    const selectedTags = this.data.tags
      .filter(item => item.selected)
      .map(item => item.name);

    const formData = {
      consultationId: this.data.consultation.id,
      rating: this.data.rating,
      tags: selectedTags,
      content: this.data.content,
      isAnonymous: this.data.isAnonymous
    };

    // 这里调用提交评价的API
    console.log('提交评价数据:', formData);
    wx.request({
      url: 'https://your-api.com/comments',
      method: 'POST',
      data: formData,
      success: (res) => {
        wx.showToast({
          title: '评价成功',
          icon: 'success'
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      },
      fail: (err) => {
        wx.showToast({
          title: '提交失败',
          icon: 'error'
        });
      }
    });
  },

  onLoad(options) {
    // 实际开发中应从服务器获取咨询详情
    // this.loadConsultationDetail(options.id);
  }
});